package com.example.neo4j.repository;

import com.example.neo4j.pojo.node.User;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;

import java.util.List;

public interface UserRepository extends Neo4jRepository<User, Long> {

    @Query("match(u:User{account:$account}) return u")
    User getUserByAccount(String account);

    @Query("CREATE (user:User{account:$account,password:$password,nickname:$nickname,age:$age,gender:$gender,email:$email,address:$address}) RETURN user")
    User adduser(String account, String password, String nickname, Integer age, String gender, String email, String address);

    @Query("match(u:User{account:$account,password:$password}) return u")
    User checkUser(String account, String password);

    @Query("match(u:User{account:$account}),(h:Hobby) where ID(h)=$hobbyid  create (u)-[r:Like]->(h) return count(r)")
    Integer likeHobby(String account, Long hobbyid);

    @Query("match(u:User{account:$account}) match (u)-[r:Like]->(h:Hobby) where ID(h) =$hobbyid  delete r return count(r)")
    Integer unlikeHobby(String account, Long hobbyid);

    @Query("match(u:User{account:$account}) match (u)-[:Follow]->(users) return users")
    List<User> getMyFollowing(String account);

    @Query("match(u:User{account:$account}) match (u)<-[:Follow]-(users) return users")
    List<User> getFollowMe(String account);

    @Query("match(u:User{account:$account}) match (u)-[:Like]->(hobbies) match(users:User)-[:Like]->(hobbies) where users.account<>$account return distinct users")
    List<User> getSameHobbyUsers(String account);

    @Query("match(u:User{account:$follower}),(f:User{account:$following}) create (u)-[rr:Follow]->(f) return count(rr)")
    Long createFollow(String follower, String following);


    @Query("match (user:User) where user.account=$follower\n" +
            "match (user2:User) where user2.account=$following\n" +
            "match (user)-[r:Follow]->(user2) delete r return count(r)")
    Long deleteRelation(String follower, String following);

    @Query("match(u:User{account:$account})-[:Follow]->(friend)-[:Follow]->(users)  where users.account<>$account  return users")
    List<User> recommendFriendsByFriend(String account);

    @Query("match(u:User{account:$account})-[rel:Praised]->(share:Share) where ID(share)=$shareid return count(rel)")
    Integer isPraised(Integer shareid, String account);

    @Query("match(u:User)-[rel:Praised]->(share:Share) where ID(share)=$shareid return count(rel)")
    Long getPraisedNumber(Integer shareid);

    @Query("match (user:User) where user.account=$account \n" +
            "match (share:Share) where ID(share)=$shareid\n" +
            "create (share)<-[praised:Praised]-(user) return ID(praised)")
    Long praisedIt(String account, Integer shareid);


    @Query("match (user:User) where user.account=$account \n" +
            "match (share:Share) where ID(share)=$shareid\n" +
            "match (share)<-[praised:Praised]-(user) delete praised return count(praised)")
    Integer canclepraised(String account, Integer shareid);
}
